package xin.printk.android.view.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatRatingBar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import xin.printk.android.R;
import xin.printk.android.data.entity.Plan;
import xin.printk.android.utils.DateUtils;

public class CompletedPlanAdapter extends AbstractRecyclerAdapter<Plan> {

    private int[] mLevelStringResId = {R.string.plan_urgent_level, R.string.plan_major_level,
            R.string.plan_normal_level, R.string.plan_irrelevant_level};

    private int[] mLevelColorResId = {R.color.red_light, R.color.orange_light,
            R.color.yellow_light, R.color.gray_light};

    public CompletedPlanAdapter(Context context, List<Plan> list) {
        super(context, list);
    }

    @Override
    public RecyclerView.ViewHolder createContentHolder(ViewGroup parent) {
        return new UndoneViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_completed_plan, parent, false));
    }

    @Override
    public void bindContentHolder(RecyclerView.ViewHolder holder, int position) {
        UndoneViewHolder viewHolder = (UndoneViewHolder) holder;
        viewHolder.title.setText(mData.get(position).getTitle());
        viewHolder.content.setText(mData.get(position).getContent());
        int level = mData.get(position).getLevel();
        if (level >= 0 && level < mLevelStringResId.length) {
            viewHolder.level.setText(mContext.getString(mLevelStringResId[level]));
            viewHolder.level.setTextColor(mContext.getResources().getColor(mLevelColorResId[level]));
        }
        String rating = mContext.getString(R.string.plan_rating) +
                String.format(Locale.getDefault(), "%1.1f", mData.get(position).getRating());
        viewHolder.rating.setText(rating);
        viewHolder.ratingBar.setRating(mData.get(position).getRating());
    }

    static class UndoneViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        TextView content;
        TextView level;
        AppCompatRatingBar ratingBar;
        TextView rating;

        UndoneViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_completed_plan_title);
            content = itemView.findViewById(R.id.tv_completed_plan_content);
            level = itemView.findViewById(R.id.tv_completed_plan_level);
            ratingBar = itemView.findViewById(R.id.rb_plan_rating);
            rating = itemView.findViewById(R.id.tv_plan_rating);
        }
    }

}
